﻿using CodeGenerator.Models;
using CodeGenerator.ProjectServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TemplateManager;

namespace CodeGenerator.Generators
{
    internal class MVC3Generator : Generator
    {
        public override void Generate(int projectId, string outputPath)
        {
            Console.WriteLine("Started  : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));

            MetaData model = new MetaData();
            model.Models = this.GetClassesByProjectId(projectId);
            model.Project = this.GetProjectByProjectId(projectId);

            Console.WriteLine("Finished : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));

            //if (Console.ReadKey().Equals(ConsoleKey.Escape))
            //{
                List<LookupClassModel> list = this.GetLookupClass(model.Models);

                MVC3DALGenerator dal = new MVC3DALGenerator();
                dal.Generate(model, list, outputPath);

                MVC3BLGenerator bl = new MVC3BLGenerator();
                bl.Generate(model, list, outputPath);

                MVC3WebGenerator web = new MVC3WebGenerator();
                web.Generate(model, list, outputPath);

                GenerateSolution(projectId, outputPath);
            //}
        }

        private IProject GenerateSolution(int projectId, string outputPath)
        {
            ProjectBLClient client = new ProjectBLClient();
            ProjectDto project = client.GetProject(new ProjectPkDto() { ProjectId = projectId }).Data;

            string projectName = project.Name;
            string solutionName = projectName;
            string mvcProjectName = projectName + ".Web";
            string blProjectName = projectName + ".Service";
            string dalProjectName = projectName + ".DAL";
            string templateDirectory = @"C:\test\Template\ComplexWebApplication1\";
            string primaryOutputDirectory = outputPath + @"\" + projectName + @"\";
            string completeProjectDirectory = outputPath + @"\Complete\";

            ComplexTemplate t = new ComplexTemplate();
            t.ProjectPath = templateDirectory;
            IProject p = t.CreateNewProject(solutionName, completeProjectDirectory,
                                            dalProjectName, blProjectName, mvcProjectName);
            p.AddFile(primaryOutputDirectory);
            return p;
        }
    }
}
